<template>
  <el-dialog
    title="JSA编号选择"
    append-to-body
    :visible.sync="jobJsaBox"
    width="50%"
    :before-close="closeJsa"
  >
    <avue-crud :option="joption"
               :table-loading="jloading"
               :data="jdata"
               :page.sync="jpage"
               v-model="jform"
               ref="jcrud"
               @search-change="jsearchChange"
               @search-reset="jsearchReset"
               @selection-change="jselectionChange"
               @current-change="jcurrentChange"
               @size-change="jsizeChange"
               @refresh-change="jrefreshChange"
               @on-load="jonLoad">
    </avue-crud>
    <div class="choose_cm_footer">
      <el-button
        type="primary"
        size="small"
        @click="chooseControlMeasure()">
        确 认
      </el-button>
      <el-button
        size="small"
        @click="cancelChooseControlMeasure()">
        取 消
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
  import {getList} from "@/api/xcgl/jsaanalysis";

  export default {
    props:{
      jobJsaBox:false
    },
    data() {
      return {
        jform: {},
        jquery: {},
        jloading: true,
        jpage: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        jselectionList: [],
        joption: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          menu:false,
          addBtn:false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "编号",
              prop: "jsaNo",
              align: "center",
              search:true,
              readonly:true,
              rules: [{
                required: true,
                message: "请输入编号",
                trigger: "blur"
              }]
            },
            {
              label: "申请日期",
              prop: "sqDate",
              align: "center",
              search:true,
              type:"date",
              format: "yyyy-MM-dd",
              valueFormat: "yyyy-MM-dd",
              rules: [{
                required: true,
                message: "请输入申请日期",
                trigger: "blur"
              }]
            },
            {
              label: "主持人",
              prop: "zcr",
              align: "center",
              rules: [{
                required: true,
                message: "请输入主持人",
                trigger: "blur"
              }]
            },
            {
              label: "作业单位",
              prop: "zydw",
              align: "center",
              rules: [{
                required: true,
                message: "请输入作业单位",
                trigger: "blur"
              }]
            },
            {
              label: "作业地点",
              prop: "zydd",
              align: "center",
              rules: [{
                required: true,
                message: "请输入作业地点",
                trigger: "blur"
              }]
            },
            {
              label: "工作内容",
              prop: "gznr",
              align: "center",
              type: "textarea",
              span:24,
              rules: [{
                required: true,
                message: "请输入工作内容",
                trigger: "blur"
              }]
            },
            {
              label: "参与人",
              prop: "cyr",
              align: "center",
              span:24,
              rules: [{
                required: true,
                message: "请输入参与人",
                trigger: "blur"
              }]
            },

          ]
        },
        jdata: []
      };
    },
    computed: {
      ids() {
        let ids = [];
        this.jselectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      // 选择JSA编号
      chooseControlMeasure() {
        if(this.jselectionList.length === 0) {
          this.$message.warning("请选择JSA编号!");
          return;
        }else if (this.jselectionList.length > 1) {
          this.$message.warning("只能选择一条记录!");
          return;
        }
        this.$emit('chooseControlMeasure', this.jselectionList);
        this.jselectionClear()
      },

      // 取消选择JSA编号
      cancelChooseControlMeasure() {
        this.jselectionClear()
        this.$emit('cancelChooseControlMeasure');
      },
      closeJsa(){
        this.jselectionClear()
        this.$emit("closeJsa")
      },
      jsearchReset() {
        this.jquery = {};
        this.jonLoad(this.jpage);
      },
      jsearchChange(params, done) {
        this.jquery = params;
        this.jpage.currentPage = 1;
        this.jonLoad(this.jpage, params);
        done();
      },
      jselectionChange(list) {
        this.jselectionList = list;
      },
      jselectionClear() {
        this.jselectionList = [];
        this.$refs.jcrud.toggleSelection();
      },
      jcurrentChange(currentPage){
        this.jpage.currentPage = currentPage;
      },
      jsizeChange(pageSize){
        this.jpage.pageSize = pageSize;
      },
      jrefreshChange() {
        this.onLoad(this.jpage, this.jquery);
      },
      jonLoad(page, params = {}) {
        this.jloading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.jquery)).then(res => {
          const data = res.data.data;
          this.jpage.total = data.total;
          this.jdata = data.records;
          this.jloading = false;
          this.jselectionClear();
        });
      }
    }
  };
</script>

<style lang="scss" scoped>

</style>
